﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using NOSDC.Repositories;
using NOSDC.Areas.Admin.Models;
using System.Web.Security;

namespace NOSDC.Areas.Admin.Controllers
{
    public class UsersController : BaseController
    {
        private readonly IUserDetails _service;

        public UsersController()
        {
            _service = new UserDetailsRepository();
        }

        public UsersController(IUserDetails service)
        {
           _service = service;
        }

        // GET: /Admin/Users/
        public ActionResult Index()
        {
            IList<UserViewModel> users = (from MembershipUser i in Membership.GetAllUsers()
                                          select new UserViewModel(i.UserName, _service.GetUserDetails((Guid)i.ProviderUserKey).FirstName, _service.GetUserDetails((Guid)i.ProviderUserKey).LastName, Roles.GetRolesForUser(i.UserName)[0])).ToList();
            return View(users);
        }

        [HttpPost]
        public ActionResult Index(IEnumerable<UserViewModel> users)
        {
            foreach (var u in from u in users from r in u.AllRoles.Where(r => r.Selected) select u)
            {
                Roles.AddUserToRole(u.Username, u.Role);
            }


            return RedirectToAction("Index");
        }
    }
}
